package com.ludomancy.sora;

import java.util.ArrayList;

import com.ludomancy.gamelib.verlet.Edge;
import com.ludomancy.gamelib.verlet.Mesh;
import com.ludomancy.gamelib.verlet.Simulation;
import com.ludomancy.gamelib.verlet.Vertex;

public class Building extends Mesh
{
	private Building()
	{
		
	}
	
	public void restrictVertices()
	{
		for (int k = 0; k < verts.length; k++)
		{
			Vertex v = verts[k];
			if(v.py > Game.FLOOR)
			{
				v.py = Game.FLOOR;
				v.old_py = v.py;
			}					
		}
	}
	
	public static Building create(Simulation sim, float x, float y, float width, float height)
	{
		Vertex[] vertices = new Vertex[4];
		vertices[0] = new Vertex(x, y, Game.ARROW_RADII);
		vertices[1] = new Vertex(x+width, y, Game.ARROW_RADII);
		vertices[2] = new Vertex(x+width, y+height, Game.ARROW_RADII);
		vertices[3] = new Vertex(x, y+height, Game.ARROW_RADII);
		
		vertices[2].fixed = true;
		vertices[3].fixed = true;
		
		Edge[] extedges = new Edge[4];
		extedges[0] = new Edge(vertices[0],vertices[1]);
		extedges[1] = new Edge(vertices[1],vertices[2]);
		extedges[2] = new Edge(vertices[2],vertices[3]);
		extedges[3] = new Edge(vertices[3],vertices[0]);
		
		Edge[] intedges = new Edge[2];
		intedges[0] = new Edge(vertices[0], vertices[2]);
		intedges[1] = new Edge(vertices[1], vertices[3]);
		
		Building m = new Building();
		m.verts = vertices;
		m.iedges = intedges;
		m.eedges = extedges;
		sim.addShape(m);
		return m;
	}
	
	public static Building create(Simulation sim, float x, float y, float width, float height, int segments)
	{
		float px = x;
		float py = y;
		float swidth = width/segments;
		float sheight = height/segments;
		Vertex[][] matrix = new Vertex[segments+1][segments+1];
		ArrayList vertices = new ArrayList();
		ArrayList internalEdges = new ArrayList();
		ArrayList externalEdges = new ArrayList();
		// create vertices
		for (int i = 0; i < segments+1; i++)
		{
			px = x;
			for (int j = 0; j < segments+1; j++)
			{
				matrix[i][j] = new Vertex(px, py, Game.ARROW_RADII);
				vertices.add(matrix[i][j]);
				if(i == segments)
					matrix[i][j].fixed = true;
				px += swidth;
			}
			py += sheight;
		}
		// now connect them
		for (int i = 0; i < matrix.length; i++)
		{
			for (int j = 0; j < matrix[0].length; j++)
			{
				Vertex v = matrix[i][j];
				if(i < segments) // vertical
				{
					if(j == 0 || j == segments)
						externalEdges.add(new Edge(v,matrix[i+1][j]));
					else
						internalEdges.add(new Edge(v,matrix[i+1][j]));
				}
				if(j < segments) // horizontal
				{
					if(i == 0 || i == segments)
						externalEdges.add(new Edge(v,matrix[i][j+1]));
					else
						internalEdges.add(new Edge(v,matrix[i][j+1]));
				}
				if(i < segments && j < segments) // diagonals
					internalEdges.add(new Edge(v,matrix[i+1][j+1]));
				if(i < segments && j > 0)
					internalEdges.add(new Edge(v,matrix[i+1][j-1]));
			}
		}
		Building m = new Building();
		m.verts = (Vertex[]) vertices.toArray(new Vertex[vertices.size()]);
		m.iedges = (Edge[]) internalEdges.toArray(new Edge[internalEdges.size()]);
		m.eedges = (Edge[]) externalEdges.toArray(new Edge[externalEdges.size()]);
		sim.addShape(m);
		return m;
	}
		
//	public void onAngryHit(Edge edge)
//	{
//		// ok. Remove the edge
//		Edge[] newEdges = new Edge[edges.length-1];
//		int ndx = 0;
//		for (int i = 0; i < edges.length; i++)
//		{
//			if(edges[i] != edge)
//			{
//				newEdges[ndx++] = edges[i];
//			}
//		}
//		edge.destroy();
//		edges = newEdges;
//		colEdges = edges;
//	}
}
